﻿using AutoMapper;
using MediatR;
using SqlSugar;
using Warehouse.Domain;
using Warehouse.ErrorCord;
using Warehouse.Write.Api.Actions.Warehouse.Command;

namespace Warehouse.Write.Api.Actions.Warehouse.CommandHandle
{
    public class AddInformationCommandHandle : IRequestHandler<AddInformationCommand, APIResult<WarehouseInformation>>
    {
        private readonly ISqlSugarClient db;
        private readonly IMapper mapper;

        public AddInformationCommandHandle(ISqlSugarClient db, IMapper mapper)
        {
            this.db = db;
            this.mapper = mapper;
        }
        public  async Task<APIResult<WarehouseInformation>> Handle(AddInformationCommand request, CancellationToken cancellationToken)
        {
            APIResult<WarehouseInformation> result = new APIResult<WarehouseInformation>();
            var list = mapper.Map<WarehouseInformation>(request);
            list.IsDel = false;
            var count = await db.Insertable(list).ExecuteCommandAsync();
            if (count < 0)
            {
                result.Code = APIEnum.Fail;
                result.Msg = "添加失败";
                return await Task.FromResult(result);
            }
            result.Code = APIEnum.Success;
            result.Msg = "添加成功";
            return await Task.FromResult(result);
        }
    }
}
